package LandR.login.loginSrc.servlet;

import token.TokenHelp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/DropServlet")
public class DropServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPut(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPut(request, response);
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=utf-8"); // 响应文本格式， json格式
        response.setCharacterEncoding("utf-8"); // 编码格式
        request.setCharacterEncoding("utf-8");
        if (request.getMethod().equals("PUT")) {
            System.out.println("开始退出登录！");
            /*
            获取前端传递的token
             */
            String token = request.getHeader("token");
            token = token.substring(token.indexOf("\"") + 1, token.lastIndexOf("\""));
            System.out.println("前端传递的token令牌是：" + token);
            TokenHelp th = new TokenHelp();
            Integer ID = -1;

            try {
                ID = th.judgeToken(token);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            /*
            测：显示现在存在的所有线程
             */
            Thread.currentThread().interrupt();
   
            th.dropTokenForAllnotes(ID);
            th.dropTokenForUsersdata(ID);
            th.dropTokenForUsers(ID);
            System.out.println("《云牍云篇程序结束》，感谢用户" + ID + "的使用！");
            PrintWriter out = response.getWriter();
            out.print("退出成功！");

        } else {
            PrintWriter out = response.getWriter();
            out.print("失败！请求方法错误！");
            System.out.println("失败！请求方法错误！");
        }
    }
}
